package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import com.itheima.utils.AliOssUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.UUID;

/**
 * 体检套餐管理
 */

@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Reference
    private SetmealService setmealService;
    @Autowired
    private JedisPool jedisPool;

    //图片上传
    @RequestMapping("/upload")
    public Result upload(@RequestParam("imgFile") MultipartFile imgFile){
        String originalFilename = imgFile.getOriginalFilename();//原始文件名
        int index = originalFilename.lastIndexOf(".");
        String suffix = originalFilename.substring(index);
        String fileName = UUID.randomUUID().toString() + suffix;
        Jedis jedis = null;
        //将图片保存到七牛云
        try{
            if(jedis == null){
                jedis = jedisPool.getResource();
            }
            String url = AliOssUtils.upload(imgFile.getBytes(), fileName);
            //文件上传成功后，需要将文件名称保存到redis中
            jedis.sadd(RedisConstant.SETMEAL_PIC_RESOURCES,url);
            return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS,url);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.PIC_UPLOAD_FAIL);
        }finally {
            if(jedis != null){
                jedis.close();
            }
        }
    }

    // 图片删除
    @DeleteMapping("/delete")
    public Result delete(@RequestParam("img") String imgFile){
        try {
            AliOssUtils.delete(imgFile);
            return new Result(true, MessageConstant.PIC_DELETE_SUCCESS);
        } catch (Exception e) {
            return new Result(false, MessageConstant.PIC_DELETE_FAIL);
        }
    }

    //新增体检套餐
    @RequestMapping("/add")
    public Result add(@RequestBody Setmeal setmeal,Integer[] checkgroupIds){
        Jedis jedis = null;
        try{
            jedis = jedisPool.getResource();
            setmealService.add(setmeal,checkgroupIds);
            //将redis中的套餐全部删除
            jedis.del("setmealList");
            return new Result(true,MessageConstant.ADD_SETMEAL_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_SETMEAL_FAIL);
        }finally {
            if(jedis != null){
                jedis.close();
            }
        }
    }

    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        return setmealService.findPage(queryPageBean);
    }

    @GetMapping("/findById")
    public Result findById(Integer id){
        try {
            Setmeal setmeal = setmealService.findById(id);
            return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,setmeal);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }

    @PutMapping
    public Result edit(@RequestBody Setmeal setmeal,Integer[] checkgroupIds){
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            setmealService.edit(setmeal,checkgroupIds);
            jedis.del("setmealList");
            return new Result(true,MessageConstant.EDIT_SETMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_SETMEAL_FAIL);
        }finally {
            if (jedis != null){
                jedis.close();
            }
        }
    }

    @DeleteMapping
    public Result delete(Integer id){
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            setmealService.delete(id);
            jedis.del("setmealList");
            return new Result(true,MessageConstant.DELETE_SETMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_SETMEAL_FAIL);
        }finally{
           if(jedis != null){
               jedis.close();
           }
        }
    }
}
